{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装工具包\n",
    "!pip install -i https://mirrors.aliyun.com/pypi/simple/ python-pptx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pptx import Presentation\n",
    "from pptx.chart.data import ChartData\n",
    "from pptx.enum.chart import XL_CHART_TYPE\n",
    "from pptx.util import Inches\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建幻灯片 ------\n",
    "prs = Presentation()\n",
    "\n",
    "slide = prs.slides.add_slide(prs.slide_layouts[5])\n",
    "\n",
    "slide.shapes.title.text = \"投票统计\"\n",
    "\n",
    "# 定义图表数据 ---------------------\n",
    "chart_data = ChartData()\n",
    "# 图表定义种类（比如：我们对一个事投赞同反对票）\n",
    "chart_data.categories = ['赞同', '反对', '中立']\n",
    "# 对应上面种类的票数\n",
    "chart_data.add_series('Series 1', (29, 21, 7))\n",
    "\n",
    "# 将图表添加到幻灯片 --------------------\n",
    "# 图表左边缘的位置，图表上边缘的位置，图表的宽度，图表的高度\n",
    "# Inches 英寸\n",
    "x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)\n",
    "# 图表显示的类型，图表位置大小，和图表数据\n",
    "slide.shapes.add_chart(\n",
    "XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data\n",
    ")\n",
    "save_dir = \"./files\"\n",
    "save_file = save_dir + '/PPT_投票统计.pptx'\n",
    "print(\"保存目录路径：\",save_dir)\n",
    "print(\"保存文件路径：\",save_file)\n",
    "# import os\n",
    "if not os.path.exists(save_dir):\n",
    "    os.mkdir(save_dir)\n",
    "prs.save(save_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
